<template>
  <el-card class="good-container">
    <template #header>
      <div class="header">
        <el-button type="primary" size="small" icon="el-icon-plus" @click="handleAdd">新建商品自提点</el-button>
      </div>
    </template>
    <el-table v-loading="loading" ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 100%">
      <el-table-column type="index" label="序号" width="55"></el-table-column>
      <!-- <el-table-column prop="id" label="商品编号"></el-table-column> -->
      <el-table-column prop="zitiName" label="自提点名称" ></el-table-column>
      <el-table-column prop="mobile" label="联系方式" ></el-table-column>
      <el-table-column prop="newArea" label="自提点地址"></el-table-column>
      <el-table-column label="操作" width="100">
        <template #default="scope">
          <a style="cursor: pointer; margin-right: 10px" @click="handleEdit(scope.row)">编辑</a>
          <a style="cursor: pointer; margin-right: 10px" @click="handleStatus(scope.row.id)">删除</a>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
   <DialogAddCategory ref='addGood' :type="type" :reload="getGoodList" />
</template>

<script>
import { onMounted, reactive, ref, toRefs } from 'vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import DialogAddCategory from '@/components/DialogAddCategory.vue'
import {getData} from '@/utils/axios'
import { store } from '@/store'
export default {
  name: 'GoodPick',
   components: {
    DialogAddCategory
  },
  setup() {
    const multipleTable = ref(null)
    const addGood = ref(null)//获取元素 ----addGood.value
    const router = useRouter()
    const state = reactive({
      loading: false,
      busbaseId:store.getters.busId,
      tableData: [], // 数据列表
      type: 'add', // 操作类型
    })
    onMounted(() => {
      getGoodList()
    })
    // 获取自提点列表
    const getGoodList = () => {
       state.loading = true;
       let {busbaseId}=state;
       getData('goods/attr/getGdsZitiAddressByBusbaseId',{busbaseId,isUse:1}).then(res=>{
            console.log(res.data)
            state.loading = false
          if(res.data.flag){
           state.tableData = res.data.data.map(item=>{
                 item.areaStore=JSON.parse(item.areaStore)
                 item.areaStore.displayName=item.areaStore.displayName.replace(/,/g,"")
                 item.newArea=item.areaStore.displayName+item.address
                 return item
            })  
            console.log(state.tableData) 
          }
      }) 
    }
     // 添加自提点
    const handleAdd = () => {
      state.type = 'add'
      addGood.value.open()
    }
    // 修改自提点
    const handleEdit = (data) => {
      state.type = 'edit'
      addGood.value.open(data)
    }
    const handleStatus = (id) => {//删除
     getData('/goods/attr/delGdsZitiAddress',{busbaseId:state.busbaseId,id}).then(res=>{
            console.log(res.data)
          if(res.data.flag){
           ElMessage.success({message:'删除成功',duration:1500})
            getGoodList()
          }
      }) 
    }
    return {
      ...toRefs(state),
      multipleTable,
      addGood,//弹出框元素
      handleAdd,//新增按钮
      handleEdit,//重新编辑按钮
      getGoodList,//获取列表
      handleStatus//删除当前列
    }
  }
}
</script>

<style scoped>
  .good-container {
    min-height: 100%;
  }
  .el-card.is-always-shadow {
    min-height: 100%!important;
  }
</style>